java-同步锁和读写锁

同步锁

convert = new Thread(){
            public void run() {
                synchronized (LOCK) {
                    while (true) {
                        try {
                            //doconvert();
                        } catch (ExceptionAbstract e1) {
                            e1.printStackTrace();
                            logger.error("线程1异常,唤醒。。");
                            notifyAll();
                        }
                        if(!flag){
                            flag = true;
                            LOCK.notify();
                                try {
                                    LOCK.wait();
                                    Thread.sleep(5000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                        }
                    }
                }
            }
    };
    send = new Thread(){
            public void run() {
                synchronized (LOCK) {
                    while (true) {
                        try {
                            dosend();
                        } catch (ExceptionAbstract e1) {
                            e1.printStackTrace();
                            logger.error("线程1异常,唤醒。。");
                            notifyAll();
                        }
                        
                        if(flag){
                            flag = false;
                            LOCK.notify();
                                try {
                                    LOCK.wait();
                                    Thread.sleep(5000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                        }
                    }
                }
            }
    };
            //convert.start();
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            send.start();
View Code

读写锁

posted @ 2013-11-12 08:37  琦琦狐  阅读(182)  评论(0编辑  收藏  举报